#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System.Linq;

#endregion

namespace WaveTech.GShield.Data
{
	public static class SettingsDAO
	{
		#region Get

		public static Setting Get()
		{
			var setting = from s in Database.DC.Settings
			              where s.Id == 1
			              select s;

			return (Setting) setting;
		}

		#endregion Get

		public static void Update(Setting setting)
		{
			/*
			Setting savedSetting = Database.DC.Settings.Single(s => s.Id == setting.Id);

			savedSetting.AdminEmail = setting.AdminEmail;
			savedSetting.AdminPassword = setting.AdminPassword;
			savedSetting.BannedSleepTimer = setting.BannedSleepTimer;
			savedSetting.EnableOtherProcessWatcher = setting.EnableOtherProcessWatcher;
			savedSetting.Hash = savedSetting.Hash;
			savedSetting.LPQ1Ans = savedSetting.LPQ1Ans;
			savedSetting.LPQ1Id = savedSetting.LPQ1Id;
			savedSetting.LPQ2Ans = savedSetting.LPQ2Ans;
			savedSetting.LPQ2Id = savedSetting.LPQ2Id;
			savedSetting.RatingsSleepTimer = savedSetting.RatingsSleepTimer;
			savedSetting.SmtpLogin = savedSetting.SmtpLogin;
			savedSetting.SmtpPassword = savedSetting.SmtpPassword;
			savedSetting.SmtpRequireLogin = savedSetting.SmtpRequireLogin;
			savedSetting.SmtpServer = savedSetting.SmtpServer;
			savedSetting.TimeSleepTimer = savedSetting.TimeSleepTimer;
			savedSetting.TrimNonExactRatingResults = savedSetting.TrimNonExactRatingResults;
			savedSetting.UsageSleepTimer = savedSetting.UsageSleepTimer;
			*/
			Database.DC.SubmitChanges();
		}
	}

	#region Old SettingsDAO

	/*
  /// <summary>
  /// Settings Data Access Objects
  /// </summary>
  public class SettingsDAO
  {
    #region Save
    /// <summary>
    /// Saves or Updates a setting object to the database
    /// </summary>
    /// <param name="setting">Setting Object to Save/Update</param>
    /// <returns>Setting object with a copy of the persisted data</returns>
    public Setting Save(Setting setting)
    {
      if (setting == null)
        throw new ArgumentNullException("setting");

      if (setting.Id == null)
        setting = Insert(setting);
      else
        setting = Update(setting);

      return setting;
    }
    #endregion Save

    #region Insert
    /// <summary>
    /// Inserts (Saves) a new Settings Object into the database.
    /// </summary>
    /// <param name="setting">Setting object to save</param>
    /// <returns>Copy of the Persisted Setting Object</returns>
    private Setting Insert(Setting setting)
    {
      if (setting == null)
        throw new ArgumentNullException("setting");
      else if (setting.Id != null)
        throw new ArgumentException("Setting.Id must be null to perform an insert.");

      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
    	dbCommand =
    		db.GetSqlStringCommand(
					@"INSERT INTO [System]
           ([AdminPassword]
           ,[LPQ1Id]
           ,[LPQ1Ans]
           ,[LPQ2Id]
           ,[LPQ2Ans]
           ,[AdminEmail]
           ,[SmtpServer]
           ,[SmtpRequireLogin]
           ,[SmtpLogin]
           ,[SmtpPassword]
           ,[EnableOtherProcessWatcher]
           ,[RatingsSleepTimer]
           ,[BannedSleepTimer]
           ,[TimeSleepTimer]
           ,[UsageSleepTimer]
					 ,[Version]
           ,[Hash]
					 ,[TrimNonExactRatingResults])
     VALUES
           (@AdminPassword
           ,@LPQ1Id
           ,@LPQ1Ans
           ,@LPQ2Id
           ,@LPQ2Ans
           ,@AdminEmail
           ,@SmtpServer
           ,@SmtpRequireLogin
           ,@SmtpLogin
           ,@SmtpPassword
           ,@EnableOtherProcessWatcher
           ,@RatingsSleepTimer
           ,@BannedSleepTimer
           ,@TimeSleepTimer
           ,@UsageSleepTimer
					 ,@Version
           ,@Hash
				   ,@TrimNonExactRatingResults)

					 SELECT Scope_Identity()");

      db.AddInParameter(dbCommand, "@AdminPassword", DbType.String, setting.AdminPassword);
      db.AddInParameter(dbCommand, "@LPQ1Id", DbType.Int32, setting.Lpq1Id);
      db.AddInParameter(dbCommand, "@LPQ1Ans", DbType.String, Encryption.Encrypt(setting.Lpq2Ans));
      db.AddInParameter(dbCommand, "@LPQ2Id", DbType.Int32, setting.Lpq2Id);
      db.AddInParameter(dbCommand, "@LPQ2Ans", DbType.String, Encryption.Encrypt(setting.Lpq2Ans));
      db.AddInParameter(dbCommand, "@AdminEmail", DbType.String, Encryption.Encrypt(setting.AdminEmail));
      db.AddInParameter(dbCommand, "@SmtpServer", DbType.String, Encryption.Encrypt(setting.SmtpServer));
      db.AddInParameter(dbCommand, "@SmtpRequireLogin", DbType.Boolean, setting.SmtpRequireLogin.Value);
      db.AddInParameter(dbCommand, "@SmtpLogin", DbType.String, Encryption.Encrypt(setting.SmtpLogin));
      db.AddInParameter(dbCommand, "@SmtpPassword", DbType.String, Encryption.Encrypt(setting.SmtpPassword));
      db.AddInParameter(dbCommand, "@EnableOtherProcessWatcher", DbType.Boolean, setting.EnableOtherProcessWatcher);
      db.AddInParameter(dbCommand, "@RatingsSleepTimer", DbType.String, Encryption.Encrypt(setting.RatingsSleepTimer.ToString()));
      db.AddInParameter(dbCommand, "@BannedSleepTimer", DbType.String, Encryption.Encrypt(setting.BannedSleepTimer.ToString()));
      db.AddInParameter(dbCommand, "@TimeSleepTimer", DbType.String, Encryption.Encrypt(setting.TimeSleepTimer.ToString()));
      db.AddInParameter(dbCommand, "@UsageSleepTimer", DbType.String, Encryption.Encrypt(setting.UsageSleepTimer.ToString()));
			db.AddInParameter(dbCommand, "@Version", DbType.String, setting.Version);
      db.AddInParameter(dbCommand, "@Hash", DbType.String, setting.ComputeHash());
			db.AddInParameter(dbCommand, "@TrimNonExactRatingResults", DbType.Boolean, setting.TrimNonExactRatingResults);

      db.ExecuteNonQuery(dbCommand);

			setting.Id = (int?)db.ExecuteScalar(dbCommand);

      return setting;
    }
    #endregion Insert

    #region Update
    /// <summary>
    /// Updates a existing setting in the database
    /// </summary>
    /// <param name="setting">Setting Object to update</param>
    /// <returns>Copy of the Persisted Setting Data</returns>
    private Setting Update(Setting setting)
    {
      if (setting == null)
        throw new ArgumentNullException("setting");
      else if (setting.Id == null)
        throw new ArgumentException("Setting.Id must be not null to perform an update.");

      Database db;
      DbCommand dbCommand;


      db = DatabaseFactory.CreateDatabase();

			if (setting.AdminPassword == null || setting.AdminPassword.Length <= 0) // The Update was passed wihout a new admin password, don't change.
			{
				dbCommand = db.GetSqlStringCommand(@"UPDATE [System]
																							SET [LPQ1Id] = @LPQ1Id
																							,[LPQ1Ans] = @LPQ1Ans
																							,[LPQ2Id] = @LPQ2Id
																							,[LPQ2Ans] = @LPQ2Ans
																							,[AdminEmail] = @AdminEmail
																							,[SmtpServer] = @SmtpServer
																							,[SmtpRequireLogin] = @SmtpRequireLogin
																							,[SmtpLogin] = @SmtpLogin
																							,[SmtpPassword] = @SmtpPassword
																							,[EnableOtherProcessWatcher] = @EnableOtherProcessWatcher
																							,[RatingsSleepTimer] = @RatingsSleepTimer
																							,[BannedSleepTimer] = @BannedSleepTimer
																							,[TimeSleepTimer] = @TimeSleepTimer
																							,[UsageSleepTimer] = @UsageSleepTimer
																							,[Version] = @Version
																							,[Hash] = @Hash
																							,[TrimNonExactRatingResults] = @TrimNonExactRatingResults
																							WHERE (Id = @Id)");
			}
			else
			{
				dbCommand = db.GetSqlStringCommand(@"UPDATE [System]
																							SET [AdminPassword] = @AdminPassword
																							,[LPQ1Id] = @LPQ1Id
																							,[LPQ1Ans] = @LPQ1Ans
																							,[LPQ2Id] = @LPQ2Id
																							,[LPQ2Ans] = @LPQ2Ans
																							,[AdminEmail] = @AdminEmail
																							,[SmtpServer] = @SmtpServer
																							,[SmtpRequireLogin] = @SmtpRequireLogin
																							,[SmtpLogin] = @SmtpLogin
																							,[SmtpPassword] = @SmtpPassword
																							,[EnableOtherProcessWatcher] = @EnableOtherProcessWatcher
																							,[RatingsSleepTimer] = @RatingsSleepTimer
																							,[BannedSleepTimer] = @BannedSleepTimer
																							,[TimeSleepTimer] = @TimeSleepTimer
																							,[UsageSleepTimer] = @UsageSleepTimer
																							,[Version] = @Version
																							,[Hash] = @Hash
																						  ,@TrimNonExactRatingResults = @TrimNonExactRatingResults
																						  WHERE (Id = @Id)");

				db.AddInParameter(dbCommand, "@AdminPassword", DbType.String, setting.AdminPassword);
			}

      db.AddInParameter(dbCommand, "@LPQ1Id", DbType.Int32, setting.Lpq1Id);
      db.AddInParameter(dbCommand, "@LPQ1Ans", DbType.String, Encryption.Encrypt(setting.Lpq1Ans));
      db.AddInParameter(dbCommand, "@LPQ2Id", DbType.Int32, setting.Lpq2Id);
      db.AddInParameter(dbCommand, "@LPQ2Ans", DbType.String, Encryption.Encrypt(setting.Lpq2Ans));
      db.AddInParameter(dbCommand, "@AdminEmail", DbType.String, Encryption.Encrypt(setting.AdminEmail));
      db.AddInParameter(dbCommand, "@SmtpServer", DbType.String, Encryption.Encrypt(setting.SmtpServer));
      db.AddInParameter(dbCommand, "@SmtpRequireLogin", DbType.Boolean, setting.SmtpRequireLogin.Value);
      db.AddInParameter(dbCommand, "@SmtpLogin", DbType.String, Encryption.Encrypt(setting.SmtpLogin));
      db.AddInParameter(dbCommand, "@SmtpPassword", DbType.String, Encryption.Encrypt(setting.SmtpPassword));
      db.AddInParameter(dbCommand, "@EnableOtherProcessWatcher", DbType.Boolean, setting.EnableOtherProcessWatcher);
      db.AddInParameter(dbCommand, "@RatingsSleepTimer", DbType.String, Encryption.Encrypt(setting.RatingsSleepTimer.ToString()));
      db.AddInParameter(dbCommand, "@BannedSleepTimer", DbType.String, Encryption.Encrypt(setting.BannedSleepTimer.ToString()));
      db.AddInParameter(dbCommand, "@TimeSleepTimer", DbType.String, Encryption.Encrypt(setting.TimeSleepTimer.ToString()));
      db.AddInParameter(dbCommand, "@UsageSleepTimer", DbType.String, Encryption.Encrypt(setting.UsageSleepTimer.ToString()));
      db.AddInParameter(dbCommand, "@Hash", DbType.String, setting.ComputeHash());
      db.AddInParameter(dbCommand, "@Id", DbType.Int32, setting.Id);
			db.AddInParameter(dbCommand, "@Version", DbType.String, setting.Version);
			db.AddInParameter(dbCommand, "@TrimNonExactRatingResults", DbType.Boolean, setting.TrimNonExactRatingResults);

      db.ExecuteNonQuery(dbCommand);

      return setting;
    }
    #endregion Update

    #region Get
    /// <summary>
    /// Get the primary setting (system) object
    /// </summary>
    /// <returns>The Primary Setting (System) Object</returns>
    public Setting Get()
    {
      Setting m_setting;
      Database db;
      DbCommand dbCommand;

      db = DatabaseFactory.CreateDatabase();
			dbCommand = db.GetSqlStringCommand(@"SELECT Id, [AdminPassword], LPQ1Id, LPQ1Ans, LPQ2Id, LPQ2Ans, AdminEmail, SmtpServer, 
																				[SmtpRequireLogin], [SmtpLogin], [SmtpPassword], [EnableOtherProcessWatcher], [RatingsSleepTimer], 
																				[BannedSleepTimer], [TimeSleepTimer], [UsageSleepTimer], [Version], [Hash], [TrimNonExactRatingResults]
																				FROM dbo.System
																				WHERE (Id = @Id)");

      db.AddInParameter(dbCommand, "@Id", DbType.Int32, "1");

      m_setting = FillSettingObject(db.ExecuteReader(dbCommand));

      return m_setting;
    }
    #endregion Get

    #region Fill Setting Object
    /// <summary>
    /// Populates the Setting Object from a DataReader
    /// </summary>
    /// <param name="reader">Reader used to populate</param>
    /// <returns>Populated Setting Object</returns>
    private Setting FillSettingObject(IDataReader reader)
    {
      if (reader == null)
        throw new ArgumentNullException("reader");

      Setting setting = null;

      if (reader.Read())
      {
        setting = new Setting();

        if (!reader.IsDBNull(reader.GetOrdinal("Id")))
          setting.Id = reader.GetInt32(reader.GetOrdinal("Id"));

        if (!reader.IsDBNull(reader.GetOrdinal("AdminPassword")))
          setting.AdminPassword = reader.GetString(reader.GetOrdinal("AdminPassword"));

        if (!reader.IsDBNull(reader.GetOrdinal("LPQ1Id")))
          setting.Lpq1Id = reader.GetInt32(reader.GetOrdinal("LPQ1Id"));

        if (!reader.IsDBNull(reader.GetOrdinal("LPQ1Ans")))
          setting.Lpq1Ans = Encryption.Decrypt(reader.GetString(reader.GetOrdinal("LPQ1Ans")));

        if (!reader.IsDBNull(reader.GetOrdinal("LPQ2Id")))
          setting.Lpq2Id = reader.GetInt32(reader.GetOrdinal("LPQ2Id"));

        if (!reader.IsDBNull(reader.GetOrdinal("LPQ2Ans")))
          setting.Lpq2Ans = Encryption.Decrypt(reader.GetString(reader.GetOrdinal("LPQ2Ans")));

        if (!reader.IsDBNull(reader.GetOrdinal("AdminEmail")))
          setting.AdminEmail = Encryption.Decrypt(reader.GetString(reader.GetOrdinal("AdminEmail")));

        if (!reader.IsDBNull(reader.GetOrdinal("SmtpServer")))
          setting.SmtpServer = Encryption.Decrypt(reader.GetString(reader.GetOrdinal("SmtpServer")));

        if (!reader.IsDBNull(reader.GetOrdinal("SmtpRequireLogin")))
          setting.SmtpRequireLogin = reader.GetBoolean(reader.GetOrdinal("SmtpRequireLogin"));

        if (!reader.IsDBNull(reader.GetOrdinal("SmtpLogin")))
          setting.SmtpLogin = Encryption.Decrypt(reader.GetString(reader.GetOrdinal("SmtpLogin")));

        if (!reader.IsDBNull(reader.GetOrdinal("SmtpPassword")))
          setting.SmtpPassword = Encryption.Decrypt(reader.GetString(reader.GetOrdinal("SmtpPassword")));

        if (!reader.IsDBNull(reader.GetOrdinal("EnableOtherProcessWatcher")))
          setting.EnableOtherProcessWatcher = reader.GetBoolean(reader.GetOrdinal("EnableOtherProcessWatcher"));

        if (!reader.IsDBNull(reader.GetOrdinal("RatingsSleepTimer")))
          setting.RatingsSleepTimer = int.Parse(Encryption.Decrypt(reader.GetString(reader.GetOrdinal("RatingsSleepTimer"))));

        if (!reader.IsDBNull(reader.GetOrdinal("BannedSleepTimer")))
          setting.BannedSleepTimer = int.Parse(Encryption.Decrypt(reader.GetString(reader.GetOrdinal("BannedSleepTimer"))));

        if (!reader.IsDBNull(reader.GetOrdinal("TimeSleepTimer")))
          setting.TimeSleepTimer = int.Parse(Encryption.Decrypt(reader.GetString(reader.GetOrdinal("TimeSleepTimer"))));

        if (!reader.IsDBNull(reader.GetOrdinal("UsageSleepTimer")))
          setting.UsageSleepTimer = int.Parse(Encryption.Decrypt(reader.GetString(reader.GetOrdinal("UsageSleepTimer"))));

				if (!reader.IsDBNull(reader.GetOrdinal("Version")))
					setting.Version = reader.GetString(reader.GetOrdinal("Version"));

        if (!reader.IsDBNull(reader.GetOrdinal("Hash")))
          setting.Hash = reader.GetString(reader.GetOrdinal("Hash"));

				if (!reader.IsDBNull(reader.GetOrdinal("TrimNonExactRatingResults")))
					setting.TrimNonExactRatingResults = reader.GetBoolean(reader.GetOrdinal("TrimNonExactRatingResults"));
      }
      return setting;
    }
    #endregion Fill Game Object
  }
    */

	#endregion Old SettingsDAO
}